package com.hqd527.qdshop.serviceproduct.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hqd527.qdshop.product.BaseTrademark;
import com.hqd527.qdshop.result.Result;
import com.hqd527.qdshop.serviceproduct.service.BaseTrademarkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author 太优秀
 * @date 2022/4/7 15:36
 * @description
 */

@RestController
@RequestMapping("/admin/product/baseTrademark")
@CrossOrigin
@Api(value="品牌管理",tags = "品牌管理")
public class BaseTrademarkManageController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    @ApiOperation("分页查询品牌列表")
    @GetMapping("/getTrademarkBypage/{current}/{limit}")
    public Result getTrademarkBypage(@PathVariable("current") Long current,
                                     @PathVariable("limit") Long limit){
        IPage<BaseTrademark> pageModel = baseTrademarkService.getTrademarkByPage(current, limit);
        // 查询出来的数据列表
        List<BaseTrademark> currentList = pageModel.getRecords();
        // 总记录数
        long total = pageModel.getTotal();
        // 封装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("currentList",currentList);
        map.put("total",total);

        return Result.ok(currentList);
    }

    // 根据品牌ID获取对应品牌
    @ApiOperation("根据品牌ID获取对应品牌")
    @GetMapping("/getTrademarkById/{id}")
    public Result getTrademarkById(@PathVariable("id") String id){
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }

    // 新增品牌
    @ApiOperation("新增品牌")
    @PostMapping("/saveTrademark")
    public Result saveTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }


    // 根据品牌ID修改品牌信息
    @ApiOperation("根据品牌ID修改品牌信息")
    @PutMapping("/updateTrademarkById")
    public Result updateTrademarkById(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }


    // 根据品牌ID删除对应品牌信息
    @ApiOperation("根据品牌ID删除对应品牌信息")
    @DeleteMapping("/removeTrademark/{id}")
    public Result removeTrademark(@PathVariable Long id) {
        baseTrademarkService.removeById(id);
        return Result.ok();
    }

}
